import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STData } from '@delon/abc/st';
import { SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { BasicTableComponent } from 'src/app/routes/commom';
import { TicketService } from '../../services/ticket.service';

@Component({
  selector: 'app-billing-order',
  templateUrl: './billing-order.component.html',
  styleUrls: ['../../../commom/less/commom-table.less', './billing-order.component.less']
})
export class BillingOrderComponent extends BasicTableComponent {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  constructor(public service: TicketService, private router: Router, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
    }
    return requestOptions;
  };

  routeTo(item: any) {
    // return;
    this.router.navigateByUrl(`/order-management/vehicle/vehicle-detail/${item.billId}`);
  }

  export() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_export_InvoicedBillInfoPage);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        serviceType: {
          title: '服务类型',
          type: 'string',
          enum: [
            {
              label: '全部',
              value: ''
            },
            {
              label: '整车抢单',
              value: 1
            },
            {
              label: '整车指派',
              value: 2
            },
            {
              label: '大宗抢单',
              value: 3
            },
            {
              label: '大宗指派',
              value: 4
            },
            {
              label: '结算单',
              value: 5
            }
          ],
          ui: {
            widget: 'select'
          }
        },
        shipperAppUserName: {
          type: 'string',
          title: '货主名称',
          ui: {
            placeholder: '请输入'
          }
        },
        vatappcode: {
          type: 'string',
          title: '申请编号',
          ui: {
            autocomplete: 'off'
          }
        },
        vatinvcode: {
          type: 'string',
          title: '分票编号',
          ui: {
            placeholder: '请输入'
          }
        },
        vatappdate: {
          title: '申请时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd HH:mm:ss'
          } as SFDateWidgetSchema
        },
        sts: {
          type: 'string',
          title: '开票状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'etc:invoicing:status' },
            placeholder: '请选择'
          }
        },
        invoiceno: {
          type: 'string',
          title: '发票号码',
          ui: {
            placeholder: '请输入',
            autocomplete: 'off'
          }
        },
        invoicedate: {
          title: '开票日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        enterpriseInfoId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '订单号', index: 'billCode', className: 'text-center', width: 180, },
      { title: '货主', index: 'shipperAppUserName', width: 250, className: 'text-center' },
      { title: '装卸货地', render: 'loadingPlace', width: 250, className: 'text-center' },
      { title: '运费明细', render: 'mybidDetailInfo', width: 220, className: 'text-center' },
      {
        title: '货物信息',
        index: 'goodsName',
        width: '180px',
        className: 'text-left',
        format: (item: any) =>
          `${item?.goodsName}/
        ${item?.weight || '0'}吨/
        ${item?.volume || '0'}方/
        ${item?.goodsNumber || '0'}件`
      },
      {
        title: '司机、车队长',
        render: 'driverName',
        className: 'text-center',
        width: '300px'
      },
      // {
      //   title: '车队长',
      //   className: 'text-center',
      //   width: '120px',
      //   index: 'carCaptainName'
      // },
      { title: '开票状态', index: 'vatappHInfoVO.vatappStsLabel', className: 'text-center', width: 120 },
      { title: '发票号码', index: 'vatappHInfoVO.invoiceNum', width: 130, className: 'text-center' },
      { title: '发票代码', index: 'vatappHInfoVO.invoiceCode', width: 130, className: 'text-center' },
      { title: '申请编号', index: 'vatappHInfoVO.vatappcode', width: 130, className: 'text-center' },
      { title: '申请时间', index: 'vatappHInfoVO.applyTime', width: 130, className: 'text-center' },
      { title: '网络货运人', index: 'enterpriseInfoName', width: 220, className: 'text-center' },
      { title: '关联运单号', index: 'wayBillCode', width: 180, className: 'text-center' },
      { title: '关联货源编号', index: 'resourceCode', width: 180, className: 'text-center' },
      { title: '装卸货时间', render: 'loadingTime', type: 'date', width: '200px', className: 'text-center' },
      // { title: '卸货时间', index: 'unloadTime', type: 'date', width: '150px', className: 'text-center' },
      { title: '录单时间', index: 'recordTime', type: 'date', className: 'text-center', width: '150px' },
    ];
  }
}
